﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    public class ManageIsMember
    {
        /// <summary>
        ///Metoda zjistuje, zda hrac s danym Id je clenem tymu.
        ///</summary>
        public static bool IsTeamMember(int TeamID, int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            var files = (from IsMember in db.IsMembers where IsMember.UserID==UserID && IsMember.TeamID==TeamID select IsMember).ToList();

            bool b;

            if (files.Count > 0)
                b = false;
            else
                b = true;

            return b;

        }
        /// <summary>
        ///Metoda zjistuje, zda hrac s danym Id patri mezi vedouci tymu.
        ///</summary>
        public static bool IsTeamLeader(int TeamID, int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            var files = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.TeamID == TeamID &&IsMember.IsLeader==true select IsMember).ToList();

            bool b = true;

            if (files.Count > 0)
                b = true;
            else
                b = false;

            return b;

        }
        /// <summary>
        ///Metoda zjistuje, zda hrac s danym Id patri mezi pokladniky tymu.
        ///</summary>
        public static bool IsTeamTreasurer(int TeamID, int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            var files = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.TeamID == TeamID && IsMember.IsTreasurer == true select IsMember).ToList();

            bool b = true;

            if (files.Count > 0)
                b = true;
            else
                b = false;

            return b;

        }
        /// <summary>
        ///Metoda priradi uzivatele k tymu.
        ///</summary>
        public static void ApproveUserToTeam(int UserID, int TeamID, bool IsLeader, bool IsTreasurer)
        {
            AppLib.IsMember join = new AppLib.IsMember();
            var db = MidApp.LinqUse.DBConn;

            join.UserID = UserID;
            join.TeamID = TeamID;
            join.IsLeader = IsLeader;
            join.IsTreasurer = IsTreasurer;

            db.IsMembers.InsertOnSubmit(join);
            db.SubmitChanges();

            //nova relace pro trasovani financi hrace v tymu
            MidApp.ManageStock.CreateNewStock(UserID, TeamID);
        }
        /// <summary>
        ///Metoda zmeni pozici uzivatele v tymu na vedouciho.
        ///</summary>
        public static void SetLeader(int UserID, int TeamID, bool IsLeader)
        {
            AppLib.IsMember promote = new AppLib.IsMember();
            var db = MidApp.LinqUse.DBConn;

            var aux = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.TeamID==TeamID select IsMember).First();
            
            aux.IsLeader = IsLeader;

            db.SubmitChanges();
            
        }
        /// <summary>
        ///Metoda zmeni pozici uzivatele v tymu na pokladnika.
        ///</summary>
        public static void SetTreasurer(int UserID, int TeamID, bool IsTreasurer)
        {
            AppLib.IsMember promote = new AppLib.IsMember();
            var db = MidApp.LinqUse.DBConn;

            var aux = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.TeamID == TeamID select IsMember).First();

            aux.IsTreasurer = IsTreasurer;

            db.SubmitChanges();

        }
        /// <summary>
        ///Metoda vrati vsechny uzivatele v pozici vedouci tymu.
        ///</summary>
        public static List<AppLib.IsMember> GetAllLeaders(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from IsMember in db.IsMembers where IsMember.TeamID == TeamID && IsMember.IsLeader==true select IsMember).ToList();
        }
        /// <summary>
        ///Metoda vrati vsechny uzivatele v pozici pokladnika tymu.
        ///</summary>
        public static List<AppLib.IsMember> GetAllTreasurers(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from IsMember in db.IsMembers where IsMember.TeamID == TeamID && IsMember.IsTreasurer == true select IsMember).ToList();
        }
        /// <summary>
        ///Metoda vrati vsechny uzivatele v pozici obycejneho clena tymu.
        ///</summary>
        public static List<AppLib.IsMember> GetAllSimpleMembers(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from IsMember in db.IsMembers where IsMember.TeamID == TeamID && IsMember.IsLeader == false && IsMember.IsTreasurer==false select IsMember).ToList();
        }
        /// <summary>
        ///Metoda vrati vsechny cleny tymu, nerozlisuje role.
        ///</summary>
        public static List<AppLib.IsMember> GetAllMembers(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from IsMember in db.IsMembers where IsMember.TeamID == TeamID select IsMember).ToList();
        }
        /// <summary>
        ///Metoda vraci pocet clenu tymu.
        ///</summary>
        public static int GetNumberOfTeamMembers(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from IsMember in db.IsMembers where IsMember.TeamID == TeamID select IsMember).Count();
        }
        /// <summary>
        ///Metoda vyradi hrace z tymu.
        ///</summary>
        public static void DisApproveUserToTeam(int UserID, int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.IsMember resign = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.TeamID == TeamID select IsMember).First();

            db.IsMembers.DeleteOnSubmit(resign);
            db.SubmitChanges();

            MidApp.ManageStock.RemoveStock(UserID, TeamID);
            MidApp.ManagePendingStockRequest.DeleteRequests(UserID, TeamID);

        }
        /// <summary>
        ///Metoda zjistuje, zda je hrac vedouci, nezalezi na tymu.
        ///</summary>
        public static bool IsLeaderToAnyTeam(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;
            bool boo;

            List<AppLib.IsMember> aux = (from IsMember in db.IsMembers where IsMember.UserID == UserID && IsMember.IsLeader==true select IsMember).ToList();

            if (aux.Count>0)
                boo = true;
            else
                boo = false;

            return boo;
        }
        /// <summary>
        ///Metoda zjistuje, zda je hrac clenem jakehokoliv tymu.
        ///</summary>
        public static bool IsMemberToAnyTeam(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;
            bool boo;

            List<AppLib.IsMember> aux = (from IsMember in db.IsMembers where IsMember.UserID == UserID select IsMember).ToList();

            if (aux.Count > 0)
                boo = true;
            else
                boo = false;

            return boo;
        }
        /// <summary>
        ///Vraci cleny, kteri nejsou prihlaseni na danou akci.
        ///</summary>
        public static List<AppLib.IsMember> GetNonParticipant(int TeamID, int EventID)
        {
            var dc = MidApp.LinqUse.DBConn;

            //sql prikaz "not in" vyjadreny v LINQ
            var query = (from c in dc.IsMembers  where !(from o in dc.Participates where o.UserID==c.UserID && o.EventID==EventID select o.UserID).Contains(c.UserID)where c.TeamID==TeamID select c).ToList();
            
            return query;
        }


    }
        
}
